//
//  IDGWBViewController.m
//  Goodman Web Browser
//
//  Created by iD Student on 7/11/13.
//  Copyright (c) 2013 Apple Inc. All rights reserved.
//

#import "IDGWBViewController.h"

#import "IDGWBHistoryViewController.h"

@interface IDGWBViewController ()

@end

@implementation IDGWBViewController{
    NSString *addressToGo;
    
}


- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{
    
    
}

- (void)webViewDidStartLoad:(UIWebView *)webView
{
    
    [_addressBar setText:[[webView.request URL] absoluteString]];
    [_activity startAnimating];
    [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
    [self setBackForwardButtonState];
    
}

- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    [self setBackForwardButtonState];
    if( ! [[[webView.request URL] absoluteString] isEqualToString:@""]) [_setHistory addObject:_addressBar.text];
    NSLog(@"%@" , _setHistory.description);
    [_activity stopAnimating];
    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
}

- (void) viewWillAppear:(BOOL)animated
{
    NSURLRequest *request = [NSURLRequest requestWithURL:self.urlToLoad];
    [_webView loadRequest:request];
}
- (void)viewDidLoad
{
    _setHistory = [[NSMutableSet alloc] init];
    [self.webView addSubview: _activity];
    [self.view addSubview: _activity];
    [self.webView setDelegate:self];
    [super viewDidLoad];
    NSString *address = @"";
    NSURL *url = [NSURL URLWithString:address];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    [_searchField setDelegate:self];
    [_addressBar setDelegate:self];
    [_webView loadRequest:request];
    [_addressBar setText:address];
}



- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    
    NSString *stringURL = _addressBar.text;
    
    

    if([[stringURL uppercaseString] hasPrefix:@"HTTP://"] || [[stringURL uppercaseString] hasPrefix:@"HTTPS://"] ){}
    else if(![[stringURL uppercaseString] hasPrefix:@"HTTP://"]) {
        
        NSString *test = [stringURL uppercaseString];
        
        stringURL = [@"http://" stringByAppendingString:stringURL];
    };
    
    
    NSURL *url = [NSURL URLWithString: stringURL];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    [_webView loadRequest:request];
    [_addressBar resignFirstResponder];
    return YES;
}



- (void) searchBarSearchButtonClicked:(UISearchBar *)searchBar{
    
    NSString *address = [@"https://www.google.com/search?q=" stringByAppendingString:_searchField.text];
    NSURL *url = [NSURL URLWithString:address];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    
    [_webView loadRequest:request];
    [_addressBar setText:address];
    [_searchField resignFirstResponder];
    
    
}

-(IBAction)search:(id)sender {
    NSURL *url = [NSURL URLWithString:_addressBar.text];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    [_webView loadRequest:request];
    [_addressBar resignFirstResponder];
    
}

- (void) setBackForwardButtonState{
    
    
    [self.buttonBack setEnabled:[_webView canGoBack]];
    [_addressBar setText:[[_webView.request URL] absoluteString]];
    
    [self.buttonForward setEnabled:[_webView canGoForward]];
    [_addressBar setText:[[_webView.request URL] absoluteString]];
    
}
- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
 
    if ([segue.identifier isEqualToString:@"segueHistory"]){
        
        
        [(IDGWBHistoryViewController *) segue.destinationViewController setArrayHistory: [_setHistory allObjects]];
        [(IDGWBHistoryViewController *) segue.destinationViewController setWebViewController:self];
    }
}

-(IBAction)homeButton:(id)sender {
    NSString *address = @"http://www.google.com";
    NSURL *url = [NSURL URLWithString:address];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    [_addressBar setText:address];
    [_webView loadRequest:request];
    
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



- (IBAction)refreshClicks:(id)sender
{
    [_webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:[@"" stringByAppendingString:[_addressBar text]]]]];
}

- (IBAction)buttonSettings:(id)sender {
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Stainless Browser" message:@"The 'Preferences' feature will be coming soon and is currently not available." delegate:self cancelButtonTitle:@"Dismiss" otherButtonTitles:nil,nil,nil];
    [alert show];
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return  [_setHistory count];
}

- (void) goBack: (id)sender{
    [_webView goBack];
}
- (void) goForward: (id)sender{
    [_webView goForward];
}

@end